/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2015 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = testtools.c                                        */
/*  NOTE      = 整体入口                                           */
/*  DATE      = 2015/11/03 by guojq                                */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <getopt.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlog.h"
#include "frlist.h"
#include "frxml_parse.h"
#include "test_comm.h"
#include "testtools_send.h"
#include "testtools_receive.h"

static pthread_t g_thread_receive = 0;

char *g_p_dumpfile = NULL;
char *g_p_save_file = NULL;
//从协议分析器接收信息的端口
static int g_i_connect_port = 10002;
//接收信息的超时时间(单位:秒)
static int g_i_timeout = 5;

static void __usage(char *p_pgname)
{
    printf("必须安装tcpreplay程序\n");
    printf("Usage: %s [OPTION]... \n", p_pgname);
    printf("param info\n");
    printf("\t-d 测试用pcap文件\n");
    printf("\t-s 测试结果保存文件\n");
    printf("\t-i 网卡设备名 (eg. -i eth2). (default eth0)\n");
    printf("\t-p 从协议分析器接收信息的端口 (default 10002)\n");
    printf("\t-t 接收信息的超时时间(单位:秒) (default 5)\n");
    printf("\n");

    printf("\t-h/--help help.\n");
}

static void *__do_receive(void *arg)
{
    testtools_receive(g_i_connect_port, g_i_timeout);

    pthread_exit(0);
    return (void*)NULL;
}

int main (int argc, char *argv[])
{
    char *p_dev;
    int i_ret_fun = 0;
    int i_flag_param = 0, i_flag_exit = FR_NO;
    char *p_short_opts = "d:s:p:t:i:h";
    struct option st_long_opts[] = { {"help", 0, NULL, 'H'},
                                     {"save", 1, NULL, 'S'},
                                     {"port", 1, NULL, 'P'},
                                     {"timeout", 1, NULL, 'T'},
                                     {NULL, 0, NULL, 0} };
    
    i_ret_fun = fr_slab_init(20971520, "testtools");//20M
    if(i_ret_fun == FR_FAIL)
    {
        frwarn("fr_slab_init err\n");
        return 1;
    }
    
    p_dev="eth0";
    while((i_flag_param = getopt_long(argc, argv, p_short_opts, st_long_opts, NULL) )!=-1)
    {
        switch(i_flag_param)
        {
            case 'd': //测试数据包
                //frdbg("option s:[%s]\n",optarg);
                g_p_dumpfile = optarg;
                break;
            case 'i': //网卡设备名
                p_dev = optarg;
                break;
            case 'S':
            case 's': //保存的测试结果
                //frdbg("option s:[%s]\n",optarg);
                g_p_save_file = optarg;
                break;
            case 'H':
            case 'h':
                i_flag_exit = FR_YES;
                break;
            case 'P':
            case 'p':
                g_i_connect_port = atoi(optarg);
            case 'T':
            case 't':
                g_i_timeout = atoi(optarg);
            default:
                break;
        }
    }
    if (g_p_dumpfile == NULL || g_p_save_file == NULL || i_flag_exit == FR_YES)
    {
        __usage(argv[0]);
        fr_slab_destroy();
        return 1;
    }
    
    //启动接收数据线程
    i_ret_fun = pthread_create(&g_thread_receive, NULL, __do_receive, NULL);
    if(i_ret_fun)
    {
        printf("pthread_create receive err\n");
        goto err;
    }
    
    //运行发包程序
    testtools_send_pcap(p_dev, g_p_dumpfile);
    
    if(g_thread_receive)
        pthread_join(g_thread_receive, NULL);
    
    fr_slab_destroy();
    
    return 0;
err:
    if(g_thread_receive)
        pthread_join(g_thread_receive, NULL);
    fr_slab_destroy();
    return 1;
}